package com.firefly.admin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.firefly.admin.common.constant.Constants;
import com.firefly.admin.common.utils.DateUtil;
import com.firefly.admin.common.utils.JwtTokenUtil;
import com.firefly.admin.system.entity.FySystemUserNotice;
import com.firefly.admin.system.mapper.FySystemUserNoticeMapper;
import com.firefly.admin.system.service.FySystemUserNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class FySystemUserNoticeServiceImpl implements FySystemUserNoticeService {

    @Autowired
    private FySystemUserNoticeMapper fySystemUserNoticeMapper;


    /**
     * 查询用户通知公告信息
     *
     * @param noticeId 通知公告ID
     * @return 用户公告信息
     */
    @Override
    public FySystemUserNotice alreadyRead(Long noticeId) {
        Long userId = JwtTokenUtil.getLoginUserId();
        LambdaQueryWrapper<FySystemUserNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FySystemUserNotice::getNoticeId, noticeId);
        queryWrapper.eq(FySystemUserNotice::getUserId, userId);
        List<FySystemUserNotice> systemUserNoticeList = fySystemUserNoticeMapper.selectList(queryWrapper);
        FySystemUserNotice fySystemUserNotice = null;
        if (!CollectionUtils.isEmpty(systemUserNoticeList)) {
            fySystemUserNotice = systemUserNoticeList.get(0);
            fySystemUserNotice.setReadTime(DateUtil.getNowDate());
            fySystemUserNotice.setAlreadyRead(Constants.MESSAGE_ALREADY_READ);
            fySystemUserNoticeMapper.updateById(fySystemUserNotice);
            return fySystemUserNotice;
        }
        return fySystemUserNotice;
    }

    /**
     * 根据用户消息ID查询用户消息信息
     *
     * @param noticeId 用户消息ID
     * @return 用户消息信息
     */
    @Override
    public FySystemUserNotice findByNoticeId(Long noticeId) {
        Long userId = JwtTokenUtil.getLoginUserId();
        LambdaQueryWrapper<FySystemUserNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FySystemUserNotice::getNoticeId, noticeId);
        queryWrapper.eq(FySystemUserNotice::getUserId, userId);
        List<FySystemUserNotice> systemUserMessageList = fySystemUserNoticeMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(systemUserMessageList)) {
            return systemUserMessageList.get(0);
        }
        return null;
    }

    /**
     * 新增用户通知公告信息
     *
     * @param userId     用户ID
     * @param noticeId   消息ID
     * @param noticeType 公告类型
     */
    @Override
    public void save(Long userId, Long noticeId, String noticeType) {
        FySystemUserNotice fySystemUserNotice = new FySystemUserNotice();
        fySystemUserNotice.setUserId(userId);
        fySystemUserNotice.setNoticeId(noticeId);
        fySystemUserNotice.setNoticeType(noticeType);
        fySystemUserNotice.setAlreadyRead(Constants.MESSAGE_ALREADY_UNREAD);
        fySystemUserNotice.setCreateTime(DateUtil.getNowDate());
        fySystemUserNoticeMapper.insert(fySystemUserNotice);
    }

    /**
     * 通过通知公告类型查询用户公告信息
     *
     * @param noticeType 公告类型
     * @return 用户公告列表
     */
    @Override
    public List<FySystemUserNotice> findByNoticeType(String noticeType) {
        Long userId = JwtTokenUtil.getLoginUserId();
        LambdaQueryWrapper<FySystemUserNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FySystemUserNotice::getNoticeType, noticeType);
        queryWrapper.eq(FySystemUserNotice::getUserId, userId);
        queryWrapper.orderByDesc(FySystemUserNotice::getAlreadyRead);
        queryWrapper.orderByDesc(FySystemUserNotice::getCreateTime);
        return fySystemUserNoticeMapper.selectList(queryWrapper);
    }

    /**
     * 查询用户未读通知公告条数
     *
     * @return 未读条数
     */
    @Override
    public int getUnReadNoticeCount() {
        int count = 0;
        LambdaQueryWrapper<FySystemUserNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FySystemUserNotice::getUserId, JwtTokenUtil.getLoginUserId());
        queryWrapper.eq(FySystemUserNotice::getAlreadyRead, Constants.MESSAGE_ALREADY_UNREAD);
        List<FySystemUserNotice> systemUserNoticeList = fySystemUserNoticeMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(systemUserNoticeList)) {
            count = systemUserNoticeList.size();
        }
        return count;
    }

    /**
     * 通过公告ID删除用户公告信息
     *
     * @param noticeId 公告ID
     * @return 操作结果
     */
    @Override
    public int deleteByNoticeId(Long noticeId) {
        LambdaQueryWrapper<FySystemUserNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FySystemUserNotice::getNoticeId, noticeId);
        return fySystemUserNoticeMapper.delete(queryWrapper);
    }

    /**
     * 通过公告ID查询用户公告信息列表
     *
     * @param noticeId 公告ID
     * @return 用户公告信息列表
     */
    @Override
    public List<FySystemUserNotice> findListByNoticeId(Long noticeId) {
        LambdaQueryWrapper<FySystemUserNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FySystemUserNotice::getNoticeId, noticeId);
        return fySystemUserNoticeMapper.selectList(queryWrapper);
    }
}
